#define _CRT_SECURE_NO_WARNINGS 1
#include <vector>
#include <algorithm>
#include <map>
#include <iostream>
using namespace std;
class A
{
public:
    bool operator()(vector<int> v1, vector<int> v2)
    {
        return v1[1] < v2[1];
    }
};
class Solution {
public:
    vector<int> findOrder(int numProject, vector<vector<int> >& groups)
    {
        sort(groups.begin(), groups.end(), A());
        for (auto& a : groups) cerr << a[0] << "," << a[1] << "|";
        cerr << endl;
        map<pair<int, int>, bool> ma;
        map<int, bool> hash;
        vector<int> ret;
        for (int i = 0;i < groups.size();i++)
        {
            if (groups[i][0] == groups[i][1]) return vector<int>();
            if (hash[groups[i][1]] == false)
            {
                ret.push_back(groups[i][1]);
                hash[groups[i][1]] = true;
            }
            if (ma[{groups[i][0], groups[i][1]}] == false)
            {
                ma[{groups[i][1], groups[i][0]}] = true;
            }
            else
            {
                return vector<int>();
            }
            if (hash[groups[i][0]] == false)
            {
                ret.push_back(groups[i][0]);
                hash[groups[i][0]] = true;
            }
        }
        for (int i = 0;i < numProject;i++)
        {
            if (hash[i] == false)
            {
                ret.push_back(i);
            }
        }
        return ret;
    }
};